/**
 * 供应商选择控件
 */
SupplierSelect = function(config) {
	this.url = config.url;
	this.width = config.width;
	this.windowWidth = config.windowWidth || 500;
	this.windowHeight = config.windowHeight || 300;
	this.avgWidth = (this.windowWidth - 89) / 3;
	this.inputId = config.inputId;
	this.windowTitle = config.title;
	this.multiple = config.multiple;
	this.viewType = 'list';
	this.selected = config.selected;
	this.init();
}

SupplierSelect.prototype = {
	init : function() {
		var selectEntity = this;
		var inputHidden = $('#' + this.inputId);

		var content_div = $('<DIV></DIV>');
		inputHidden.after(content_div);

		this.link_div = $('<DIV class="link_content"></DIV>');
		content_div.append(this.link_div);
		this.link_div.width(this.width - 19);

		this.link_hider = $('<DIV class="link_content_hider"></DIV>');
		this.link_hider.appendTo(this.link_div);
		this.link_hider.width(this.width - 19);
		this.link_inner = $('<DIV class="link_content_inner"></DIV>');
		this.link_inner.css('width', 1000);
		this.link_inner.appendTo(this.link_hider);

		var but = $('<DIV class="supplierSelectButton"></DIV>');
		this.link_div.after(but);

		but.click(function() {
			selectEntity.openDataSelectWindow();
		}).mousedown(function() {
			$(this).addClass('supplierSelectButtonDown').mouseout(function() {
				$(this).removeClass('supplierSelectButtonDown');
			});
		}).mouseup(function() {
			$(this).removeClass('supplierSelectButtonDown');
		});
		if (this.selected) {
			$.ajax({
				url : projectName + '/system/ajax/findSupplierByIds.t4m',
				data : 'ids=' + this.selected,
				success : function(json) {
					selectEntity.initChoose(json);
				}
			});
		}
	},
	openDataSelectWindow : function() {
		var supplierSelectEntity = this;
		this.supplierPanel = new WindowPanel({
			id : 'supplierSelect' + this.inputId,
			title : this.windowTitle,
			width : this.windowWidth,
			height : this.windowHeight,
			maximizable : false,
			draggable : false,
			html : this.showData(),
			tbar : new Toolbar({
				id : '__supplier_select',
				icon : '../../image/op.gif',
				items : [
						{
							id : 'supplierFilter',
							type : 'textfield',
							width : 80,
							keyup : function() {
								supplierSelectEntity.supplierPanel.jContent
										.html(supplierSelectEntity
												.showData('supplierName='
														+ $('#supplierFilter')
																.val()));
								supplierSelectEntity.itemEvent();
							}
						}, '-', {
							type : 'button',
							text : '确定',
							position : {
								a : '-80px -100px'
							},
							handler : function() {
								supplierSelectEntity.ok();
							}
						}, '-', {
							type : 'button',
							text : '新增供应商',
							position : {
								a : '-80px -100px'
							},
							handler : function() {
								top.supplier.add();
							}
						} ]
			})
		});

		if (this.multiple) {
			this.supplierPanel.tbar.add({
				type : 'button',
				text : '全选',
				position : {
					a : '-60px -100px'
				},
				handler : function() {
					supplierSelectEntity.chooseAll();
				}
			});
		}
		this.supplierPanel.tbar.add({
			type : 'button',
			text : '清空',
			position : {
				a : '0px -20px'
			},
			handler : function() {
				supplierSelectEntity.clear();
			}
		});
		this.itemEvent();
		$(
				'#jWindowPanel-supplierSelect' + supplierSelectEntity.inputId
						+ ' .headimg').load(function() {
			supplierSelectEntity.rebuildPosition($(this));
		});
	},
	rebuildPosition : function(_m) {
		var pSize = this.viewType === 'grid' ? 54 : 20;
		var iSize = this.viewType === 'grid' ? 50 : 16;
		if (_m.height() < _m.width()) {
			_m.css('margin-top', (pSize - _m.height()) / 2);
		}
	},
	itemEvent : function() {
		var supplierSelectEntity = this;
		// 获得所有选项元素
		var items = $('.content-body-item-' + this.viewType);
		// 添加hover事件
		items.hover(function() {
			$(this).addClass('content-body-item-hover');
		}, function() {
			$(this).removeClass('content-body-item-hover');
		});
		// 添加点击事件
		if (this.multiple) {
			items.click(function() {
				var _i = $(this);
				if (_i.hasClass('content-body-item-click')) {
					_i.removeClass('content-body-item-click');
					_i.children('.selected').remove();
				} else {
					_i.addClass('content-body-item-click');
					_i.append('<img src="' + projectName
							+ '/image/SupplierSelect/'
							+ supplierSelectEntity.viewType
							+ '_selected.gif" class="selected"/>');
				}
			});
		} else {
			items.click(function() {
				var _i = $(this);
				$('.content-body-item-click').removeClass(
						'content-body-item-click').children('.selected')
						.remove();
				if (!_i.hasClass('content-body-item-click')) {
					_i.addClass('content-body-item-click');
					_i.append('<img src="' + projectName
							+ '/image/SupplierSelect/'
							+ supplierSelectEntity.viewType
							+ '_selected.gif" class="selected"/>');
				}
			});
		}
		// 勾选已选中的
		var ids = document.getElementById(this.inputId).value.split(',');
		for ( var i = ids.length; i >= 0; i--) {
			if (ids[i]) {
				$('#' + ids[i]).addClass('content-body-item-click').append(
						'<img src="' + projectName + '/image/SupplierSelect/'
								+ supplierSelectEntity.viewType
								+ '_selected.gif" class="selected"/>');
			}
		}
	},
	showData : function(params) {
		var supplierSelectEntity = this;
		var tables = new Array();
		tables.push('<div class="supplier-select-content">');
		$.ajax({
			url : this.url,
			data : params,
			cache : false,
			async : false,
			success : function(json) {
				var _supplierList_ = json;
				var tempId = '';
				for ( var i = 0; i < _supplierList_.length; i++) {
					tables.push('<div id="' + _supplierList_[i].id
							+ '" class="content-body-item-'
							+ supplierSelectEntity.viewType + '" title="'
							+ _supplierList_[i].name + '">');
					tables.push('<div class="content-body-item-body">');
					tables.push('<div class="content-body-item-body-img">');
					tables.push('</div>');
					tables.push('<div class="content-body-item-body-info">');
					tables.push('<div style="width:'
							+ supplierSelectEntity.avgWidth
							+ 'px;" class="info-bold">'
							+ _supplierList_[i].name + '</div>');
					tables.push('<div style="width:'
							+ supplierSelectEntity.avgWidth + 'px;">'
							+ _supplierList_[i].phone + '</div>');
					tables.push('<div style="width:'
							+ supplierSelectEntity.avgWidth + 'px;">'
							+ _supplierList_[i].linkman + '</div>');
					tables.push('</div>');
					tables.push('</div>');
					tables.push('</div>');
				}
			}
		});
		tables.push('</div>');
		return tables.join('');
	},
	choose : function() {
		this.link_inner.html('');
		var list = $('.content-body-item-click');
		var values = new Array();
		var links = new Array();
		var supplierSelectEntity = this;
		for ( var i = 0; i < list.length; i++) {
			var s_id = list.eq(i).attr('id');
			var s_name = list.eq(i).attr('title');
			values.push(s_id);
			var s_supplier = this.getSupplierLink({
				supplierId : s_id,
				supplierName : s_name,
				showInfo : true
			});
			var s_html = $('<DIV id="' + this.inputId + '_' + s_id + '"></DIV>');
			s_html.append(s_supplier[0]);
			s_html.append(s_supplier[1]);
			this.link_inner.append(s_html);
		}
		$('#' + this.inputId).val(values.join(','));
		this.update();
	},
	initChoose : function(list) {
		this.link_inner.html('');
		var values = new Array();
		var links = new Array();
		var supplierSelectEntity = this;
		for ( var i = 0; i < list.length; i++) {
			var s_id = list[i].id;
			var s_name = list[i].name;
			values.push(s_id);
			var s_supplier = this.getSupplierLink({
				supplierId : s_id,
				supplierName : s_name,
				showInfo : true
			});
			var s_html = $('<DIV id="' + this.inputId + '_' + s_id + '"></DIV>');
			s_html.append(s_supplier[0]);
			s_html.append(s_supplier[1]);
			this.link_inner.append(s_html);
		}
		$('#' + this.inputId).val(values.join(','));
		this.update();
	},
	ok : function() {
		this.choose();
		WPS['jWindowPanel-supplierSelect' + this.inputId].close();
	},
	chooseAll : function() {
		var list = $('.content-body-item-' + this.viewType);
		for ( var i = 0; i < list.length; i++) {
			if (!list.eq(i).hasClass('content-body-item-click')) {
				list.eq(i).addClass('content-body-item-click');
				list.eq(i).append(
						'<img src="' + projectName + '/image/SupplierSelect/'
								+ this.viewType
								+ '_selected.gif" class="selected"/>');
			}
		}
	},
	clear : function() {
		$('.content-body-item-click').removeClass('content-body-item-click')
				.children('.selected').remove();
	},
	update : function() {
		if (this.multiple) {
			// 判断是否溢出
			var linkas_widths = 0;
			this.link_inner.children('DIV').each(function() {
				linkas_widths += $(this).outerWidth(true);
			});
			if (linkas_widths > this.link_div.width()) {
				if (!this.hasMore) {
					var supplierNames = [];
					this.link_inner.find('.supplier_selected').each(function() {
						supplierNames.push($(this).text());
					});
					this.link_div
							.append((this.link_fix = $('<div class="more" title="'
									+ supplierNames.join(',') + '">...</div>')));
					this.hasMore = true;
				}
				this.link_hider.width(this.link_div.width()
						- this.link_fix.outerWidth());
			} else {
				this.link_hider.width(this.link_div.width());
				if (this.hasMore) {
					this.link_fix.remove();
					this.hasMore = false;
				}
			}
		}
	},
	removeSupplier : function(uid) {
		var hInput = $('#' + this.inputId);
		hInput.val(hInput.val().replace(uid, '').replace(',,', ','));
		$('#' + this.inputId + '_' + uid).remove();
		this.update();
	},
	getSupplierLink : function(config) {
		var entity = this;
		var showInfo = config.showInfo ? 'void(0);' : 'void(0);';
		var supplier = $('<div class="supplier_selected" onclick="' + showInfo
				+ '">' + config.supplierName + '</div>');
		var s_delete = $('<div title="取消\'' + config.supplierName
				+ '\'" class="closer"></div>');
		s_delete.click(function() {
			entity.removeSupplier(config.supplierId);
		});
		return [ supplier, s_delete ];
	}
}